package model

import (
	"database/sql"
	"errors"
	"fmt"
	g "github.com/doug-martin/goqu/v9"
	"github.com/valyala/fasthttp"
	"strconv"
)

type TblOperatorInfoFake struct {
	Id             int     `json:"id" db:"id" cbor:"id"`
	Game           string  `json:"game" db:"game" cbor:"game"`
	OperatorId     string  `json:"operator_id" db:"operator_id"`           // 渠道id
	OperatorName   string  `json:"operator_name" db:"operator_name"`       // 渠道名称
	IntoFakeRate   int     `json:"into_fake_rate" db:"into_fake_rate"`     // 进入概率
	InitRtp        float64 `json:"init_rtp" db:"init_rtp"`                 // 初始RTP
	AdjustRtp      float64 `json:"adjust_rtp" db:"adjust_rtp"`             // 调控RTP
	AdjustMoneyMul float64 `json:"adjust_money_mul" db:"adjust_money_mul"` // 调控金额倍数
}

type TblOperatorInfoFakeParam struct {
	Page     int `json:"page"`
	PageSize int `json:"page_size"`

	Id             int     `json:"id" db:"id" cbor:"id"`
	Game           string  `json:"game" db:"game" cbor:"game"`
	OperatorId     string  `json:"operator_id" db:"operator_id"`           // 渠道id
	OperatorName   string  `json:"operator_name" db:"operator_name"`       // 渠道名称
	IntoFakeRate   int     `json:"into_fake_rate" db:"into_fake_rate"`     // 进入概率
	InitRtp        float64 `json:"init_rtp" db:"init_rtp"`                 // 初始RTP
	AdjustRtp      float64 `json:"adjust_rtp" db:"adjust_rtp"`             // 调控RTP
	AdjustMoneyMul float64 `json:"adjust_money_mul" db:"adjust_money_mul"` // 调控金额倍数
}

type TblOperatorInfoFakeList struct {
	D []TblOperatorInfoFake `json:"d" cbor:"d"`
	T int                   `json:"total" cbor:"t"`
}

func GetTblOperatorInfoFakeList(param TblOperatorInfoFakeParam, ctx *fasthttp.RequestCtx) (TblOperatorInfoFakeList, error) {
	data := TblOperatorInfoFakeList{}
	table := "tbl_operator_info_fake"
	where := "1=1"
	join := ""
	oderBy := "a.id"
	oderType := "desc"
	if param.Game != "" {
		where += " and a.game='" + param.Game + "'"
	}
	if param.OperatorId != "" {
		where += " and a.operator_id='" + param.OperatorId + "'"
	}
	join += " left join tbl_operator_info as b on b.id=a.operator_id" //渠道表
	order := oderBy + " " + oderType

	offset := (param.Page - 1) * param.PageSize
	if param.Page >= 1 {
		count := "select count(1) from " + table + " as a where " + where
		err := meta.MerchantDB.Get(&data.T, count)
		if err != nil {
			return data, pushLog(fmt.Errorf("%s,[%s]", err.Error(), count), "数据库错误")
		}
		if data.T == 0 {
			return data, nil
		}
	}

	field := "a.*,ifnull(b.operator_name,'') operator_name"
	query := "select " + field + " from " + table + "  as a " + join + " where " + where + " order by " + order + " limit " + strconv.Itoa(param.PageSize) + " offset " + strconv.Itoa(offset)
	err := meta.MerchantDB.Select(&data.D, query)
	if err != nil && err != sql.ErrNoRows {
		return data, pushLog(fmt.Errorf("%s,[%s]", err.Error(), query), "数据库错误")
	}
	return data, nil
}

func UpdateOrDelTblOperatorInfoFake(param TblOperatorInfoFake, ctx *fasthttp.RequestCtx, isDelete int) error {
	loginUser := GetLoginUser(ctx)
	if loginUser.Admin == "" {
		return errors.New("权限不足")
	}
	table := "tbl_operator_info_fake"
	query := ""
	record := g.Record{
		"into_fake_rate":   param.IntoFakeRate,
		"init_rtp":         param.InitRtp,
		"adjust_rtp":       param.AdjustRtp,
		"adjust_money_mul": param.AdjustMoneyMul,
	}
	if param.Id > 0 {
		if isDelete == 0 {
			query, _, _ = dialect.Update(table).Set(record).Where(g.Ex{"id": param.Id}).ToSQL()
		} else {
			query, _, _ = dialect.Delete(table).Where(g.Ex{"id": param.Id}).ToSQL()
		}
	} else {
		query, _, _ = dialect.Insert(table).Rows(record).ToSQL()
	}
	_, err := meta.MerchantDB.Exec(query)
	if err != nil {
		return errors.New("数据库错误")
	}
	return nil
}
